package com.thirteengroup.casemg.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.thirteengroup.casemg.Mapper.departmentMapper;
import com.thirteengroup.casemg.domain.*;
import com.thirteengroup.casemg.service.IDepartmentService;
import com.thirteengroup.casemg.service.impl.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping(value = "/employee")
public class employeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
//    private departmentMapper departmentMapper;
    private IDepartmentService departmentService;

    @RequestMapping(value = "/listAll")
    @ResponseBody
    public JasonResult list(){

        List<Employee> list=employeeService.queryAllEmployee();
       JasonResult jasonResult=new JasonResult("查询全部成功",true,list);
        return jasonResult;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JasonResult delete(Long id){
        employeeService.deleteById(id);

        JasonResult jasonResult=new JasonResult("删除成功",true);
        return jasonResult;
    }

    @RequestMapping(value = "/input")
    public ModelAndView input(Long id){
        ModelAndView mv=new ModelAndView();
        if(id!=null){
            Employee employee=employeeService.slectOne(id);
            mv.addObject("em",employee);

        }
        mv.setViewName("employee/input");
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/upOrin")
    public JasonResult upOrin(Employee employee){
//        System.out.println("employee"+employee);
//        &&employee.getDept().getId()==null
        if(employee.getId()==null){
            Department department = departmentService.slectOne(employee.getDeptid());
            employee.setDept(department);
            employeeService.insertDept(employee);
            JasonResult jasonResult=new JasonResult("添加员工信息成功",true);
            return jasonResult;
        }else {
            Department department = departmentService.slectOne(employee.getDeptid());
            employee.setDept(department);
            employeeService.updateDept(employee);
            JasonResult jasonResult=new JasonResult("编辑员工信息成功",true);
            return jasonResult;
        }

    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public JasonResult listLimit(queryObject queryObject){
//        System.out.println(queryObject.getDeptid());

        PageResult<Employee> employeePageResult=employeeService.selectLimit(queryObject);
        JasonResult jasonResult=new JasonResult("查询员工成功",true,employeePageResult);
        return jasonResult;
//        if (queryObject.getDeptid()==null){
//            PageResult<Employee> employeePageResult=employeeService.selectLimit(queryObject);
//            JasonResult jasonResult=new JasonResult("查询员工成功",true,employeePageResult);
//            return jasonResult;
//        }else {
//            if (queryObject.getDeptid()==-1){
//                PageResult<Employee> employeePageResult=employeeService.selectLimit(queryObject);
//                JasonResult jasonResult=new JasonResult("查询员工成功",true,employeePageResult);
//                return jasonResult;
//            }else {
//                PageResult<Employee> employeePageResult=employeeService.selectLimit(queryObject);
//                JasonResult jasonResult=new JasonResult("查询员工成功",true,employeePageResult);
//                return jasonResult;
//            }
//        }

    }


    @RequestMapping("/batchdelete")
    @ResponseBody
    public JasonResult batchDelete(Long [] ids){
        for (Long id:ids) {
            employeeService.deleteById(id);
        }
        JasonResult jasonResult=new JasonResult("删除成功",true);
        return jasonResult;
    }


    @RequestMapping(value = "/import")
    @ResponseBody
    public JasonResult importDta(MultipartFile pic) throws Exception{
        JasonResult jasonResult = null;
        //创建工作薄

            ExcelReaderBuilder workBook = EasyExcel.read(pic.getInputStream(),Employee.class, (ReadListener) new EmployeeReadListener(employeeService));
            //创建工作表对象
            ExcelReaderSheetBuilder sheet1=workBook.sheet();
            //读excel

        jasonResult=new JasonResult("导入成功",true);
        return jasonResult;
    }

    @RequestMapping(value = "/export")
    public void exportData(queryObject queryObject, HttpServletResponse response) throws Exception{

        List<Employee> employeeList=employeeService.queryAllEmployee();

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");

            String filename= URLEncoder.encode("员工数据表","UTF-8");
            response.setHeader("Content-disposition","attachment;filename="+filename+".xlsx");
            //创建工作薄对象
            ExcelWriterBuilder workbook=EasyExcel.write(response.getOutputStream(),Employee.class);
            ExcelWriterSheetBuilder sheet= workbook.sheet("员工数据表");
            sheet.doWrite(employeeList);


    }

//    @RequestMapping(value = "/test")
//    public String test(test abs){
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("classpath:SpringMvc.xml");
//        String [] s=applicationContext.getBeanDefinitionNames();
//        for (String aa:s
//             ) {
//            System.out.println(aa);
//        }
//        System.out.println(abs);
//        return "index";
//    }
}
